package com.example.online_order.service.impl;

import com.example.online_order.Eception.BussinessException;
import com.example.online_order.Eception.ExceptionType;
import com.example.online_order.mapper.AdminAccountMapper;
import com.example.online_order.pojo.AdminAccount;
import com.example.online_order.service.AdminAcountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class AdminAccountServiceImpl implements AdminAcountService {


    @Autowired
    AdminAccountMapper mapper;

    @Transactional
    @Override
    public Integer addAdmin(AdminAccount account) {
        AdminAccount accountDb = mapper.findAdminByUsername(account.getUserName());
        if (accountDb != null){
            throw new BussinessException(ExceptionType.bus_id_exists,"用户名已存在");
        }else {
            Integer res = mapper.addAdmin(account);
            if (res != 1){
                throw new BussinessException(ExceptionType.bus_insert_fail,"添加管理员失败");
            }else {
                return res;
            }
        }

    }


    @Transactional
    @Override
    public Integer deleteAdmin(Integer id) {
        AdminAccount accountDb = mapper.findAdminById(id);
        if (accountDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else {
            Integer res = mapper.deleteAdmin(id);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_delete_fail,"删除管理员失败");
            }else {
                return res;
            }
        }

    }


    @Transactional
    @Override
    public Integer updateAdmin(AdminAccount account) {
        AdminAccount accountDb = mapper.findAdminById(account.getId());
        if (accountDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else {
            Integer res = mapper.updateAdmin(account);
            if (res!= 1){
                throw new BussinessException(ExceptionType.bus_update_fail,"修改管理员失败");
            }else {
                return res;
            }
        }

    }

    @Override
    public List<AdminAccount> getAllAdmin() {
        return mapper.getAllAdmin();
    }

    @Override
    public AdminAccount findAdminByUsername(String username) {
        AdminAccount accountDb = mapper.findAdminByUsername(username);
        if(accountDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else{
            return accountDb;
        }
    }

    @Override
    public AdminAccount findAdminLikeUsername(String username) {
        AdminAccount accountDb = mapper.findAdminByUsername(username);
        if(accountDb == null) {
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else{
            return accountDb;
        }
    }

    @Override
    public AdminAccount Login(AdminAccount account) {
        AdminAccount accountDb = mapper.findAdminByUsername(account.getUserName());
        System.out.println(accountDb);
        if (accountDb == null) {
            throw new BussinessException(ExceptionType.bus_invalid_username,"username 错误！");
        }else{
            if (accountDb.getPassword().equals(account.getPassword())){
                return accountDb;
            }else{
                throw new BussinessException(ExceptionType.bus_invalid_password,"password 错误！");
            }
        }
    }


    @Transactional
    @Override
    public Integer updateAdminpassword(AdminAccount account) {
        AdminAccount accountDb = mapper.findAdminByUsername(account.getUserName());
        if (accountDb == null){
            throw new BussinessException(ExceptionType.bus_id_not_found,"用户不存在");
        }else {
            if (accountDb.getPhone().equals(account.getPhone())){
                accountDb.setPassword(account.getPassword());
                Integer res = mapper.updateAdminPassword(accountDb);
                if (res!= 1){
                    throw new BussinessException(ExceptionType.bus_update_fail,"修改管理员失败");
                }else {
                    return res;
                }
            }else{
                throw new BussinessException(ExceptionType.bus_invalid_phone,"phone 错误！");
            }

        }
    }
}
